package com.ydkfjs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydkfjs.model.dto.TypeRequest;
import com.ydkfjs.model.po.Item;
import com.ydkfjs.model.po.Type;
import com.ydkfjs.service.TypeService;
import com.ydkfjs.mapper.TypeMapper;
import com.ydkfjs.utils.HBAssert;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hb
 * @description 针对表【t_type】的数据库操作Service实现
 * @createDate 2024-04-02 10:34:56
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
        implements TypeService {

    private final TypeMapper typeMapper;

    public TypeServiceImpl(TypeMapper typeMapper) {
        this.typeMapper = typeMapper;
    }

    @Override
    public void add(TypeRequest typeRequest) {
        // DB类型转换
        Type type = BeanUtil.copyProperties(typeRequest, Type.class);

        // 设置更新时间
        type.setUpdateTime(LocalDateTime.now());

        // 存入数据库
        boolean save = this.save(type);

        // 判断是否操作成功
        HBAssert.isTrue(save, "添加失败");
    }

    @Override
    public List<Type> get(Integer kind) {
        // 根据类型字段查询
        List<Type> types = typeMapper.selectList(new LambdaQueryWrapper<Type>()
                .eq(Type::getKind, kind));

        return types;
    }

    @Override
    public void delete(Long id) {
        // 更具id删除
        boolean b = this.removeById(id);

        // 判断是否操作成功
        HBAssert.isTrue(b,"删除失败");
    }
}




